
$(document).ready(function() {

  jQuery.fn.changeType = function(container,url){ 
  $.ajax({
      type: "GET",
      url:  url,
      dataType: "json",
      data: "action=LIST",
      success: function(data){
      if(data != '') { 
        var options_departments = '<option class="dropDown">Select<\/option>';
		    $.each(data, function(i,d){
			   options_departments += '<option class="dropDown" value="' + d.id + '">' + d.description + '<\/option>';
		      });
		    container.html(options_departments);
		  }
        else{
          alert("Error");
        }
      },
      error: function(xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.statusText);
        alert(thrownError);
      }


    });
  };
  
  
  
  $("#account").changeType($("#account"),'/apps/script/add_account.php');    
});



$(function() {
		function log( id , description ) {
		  $("#label_for_log").show();
		  $( "<div>" ).html( "<input type='text' name='lname' disabled='disabled' value='"+description+"'  id='"+id+"'>" ).prependTo( "#log" );

			$( "#log" ).attr( "scrollTop", 0 );
		}

		$( "#city" ).autocomplete({
			source: function( request, response ) {
				$.ajax({
				type: "GET",
					url: "/apps/script/category.php",
					dataType: "json",
					data: {
						featureClass: "P",
						style: "full",
						maxRows: 12,
						name_startsWith: request.term,
						action: "SEARCH"
					},
					success: function( data ) {
						response( $.map( data, function( item ) {
							return {
								label: item.description,
								value: item.description,
								id:    item.id
							}
						}));
					},
      error: function(xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.statusText);
        alert(thrownError);
      }
				});
			},
			minLength: 2,
			select: function( event, ui ) {
				log( ui.item ? ui.item.id : "Nothing selected, input was " + this.value , ui.item ? ui.item.value : "Nothing selected, input was " + this.value);
			},
			open: function() {
				$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
			},
			close: function() {
				$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
			}
		});
	});